/**
 * Created by jeryzym on 2017/6/30.
 */
define(['knockout',
    'platform.common',
    'bootbox',
    'knockout-bsTable',
    'knockout-component',
    'knockout-fileinput',
    'sco.msg'], function (ko, platform) {
    /**
     * 与服务端方法交互
     * @type {{get: get, remove: remove, save: save}}
     */
    var rest = {
        remove: function (id) {

        }
    };

    var kbTable = require('knockout-bsTable');
    var kbFileInput = require('knockout-fileinput');

    var ViewModel = function () {
        var self = this;
        self.columns = [{
            checkbox: true,
            width: 30
        }, {
            field: 'attachment_id',
            title: '附件主键标识',
            visible: false
        }, {
            field: 'attachment_name',
            title: '附件名'
        }, {
            field: 'attachment_type',
            title: '类型'
        }, {
            field: 'upload_time',
            title: '上传时间'
        }, {
            field: 'effective_status',
            title: '生效状态',
            visible: false
        }, {
            field: 'remarks',
            title: '备注',
            visible: false
        }];
        self.tableViewModel = new kbTable.bootstrapTableViewModel({
            url: platform.mapUrl('~/rest/attachment/list'),
            toolbar: '#toolbar',
            queryParams: function (params) {
                return {
                    pageNumber: params.offset / params.limit + 1,
                    pageSize: params.limit
                };
            },
            columns: self.columns
        });

        self.modalVisible = ko.observable(false);
        self.attachMentViewModel = {
            title: ko.observable(),
            attachmentId: ko.observable(),
            attachmentName: ko.observable(),
            showNewForm: function () {
                this.title('附件上传');
                self.modalVisible(true);
                $('#attachMentDialog').toggleClass('hide');
            },
            upload: function () {
                $('#uploadFile').fileinput('upload');
            },
            onFileuploaded: function (event, data, previewId, index) {
                self.tableViewModel.refresh();
            },
            close: function () {
                $('#uploadFile').fileinput('clear');

                self.modalVisible(false);
                $('#attachMentDialog').toggleClass('hide');
            }

        };
        self.attachMentViewModel.uploadFileModel = new kbFileInput.fileInput('~/rest/attachment/uploadFile', {
            uploadExtraData: function (previewId, index) {
                var obj = {};
                return obj;
            }
        });
        self.fnSearch = function () {
            platform.showWarnTip("功能未开放");
        };
        self.fnAdd = function () {
            this.attachMentViewModel.showNewForm();
        };
        self.fnRemove = function () {

        };
    };
    return new ViewModel();
});